{layout name="layout" /}
<h2 style="text-align: center;">新闻数据</h2>
<table id="demo" lay-filter="test" class="layui-hide"></table>
<!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  <legend>总页数低于页码总数</legend>
</fieldset> -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
      <button class="layui-btn layui-btn-lg layui-btn-danger" lay-event="delete" name="buttonplay">删除</button>
      <button class="layui-btn layui-btn-lg" lay-event="update" name="buttonplay">编辑</button>
      <!-- href="javascript:void(0)" -->
    </div>
  </script>
<script>


    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#demo'
            , url: 'tablemsg'
            // , toolbar: true
            , cellMinWidth: 80
            , page: false //开启分页
            , limit: 5
            , cols: [[
                { field: 'id', title: 'ID', sort: true, width: 80 }
                , { field: 'name', title: '用户名', width: 100 }
                , { field: 'time', title: '时间', width: 200 }
                , { field: 'title', title: '标题', width: 150 }
                , { field: 'type', title: '政策', width: 150 }
                , { field: 'frequency', title: '点击量', sort: true, align: 'center', width: 100 }
                , { field: 'desc', title: '内容' }
                , { fixed: 'right', title: '操作', toolbar: '#toolbarDemo', width: 300 }
            ]]
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data;
            var type = data.type == '最新新闻' ? 1 : 2;
            if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'deladminmsg?id=' + data.id,
                        dataType: 'json',
                        async: false,
                        success: function (msg) {
                            if (msg.code == 1) {
                                obj.del();
                                layer.msg('删除成功!', { icon: 1, time: 1000 });
                            } else {
                                layer.msg('程序异常!', { icon: 5, time: 1000 });
                            }
                            layer.close(index);
                        },
                        error: function (msg) {
                            layer.msg('程序异常!', { icon: 5, time: 1000 });
                            layer.close(index);
                        }
                    });
                });
            } else if (obj.event === 'update') {
                layer.open({
                    id: 1,
                    type: 1,
                    title: '修改信息',
                    skin: 'layui-layer-rim',
                    area: ['400px', '500px'],
                    id:'tabledata',
                    content: `
                            <form id="updatafrom">
                                <div class="form-group">
                                  <label for="exampleInputEmail1">标题</label>
                                  <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="title" value="${data.title}" name="title">
                                </div>
                                <div class="form-group">
                                  <label for="exampleFormControlSelect1">类型</label>
                                  <select class="form-control" id="exampleFormControlSelect1" name="type">
                                    <option value="1">最新新闻</option>
                                    <option value="2">优惠政策</option>
                                  </select>
                                </div>
                                <div class="form-group">
                                   <label for="exampleFormControlTextarea1">内容</label>
                                   <textarea class="form-control" id="exampleFormControlTextarea1" rows="7" name="desc">${data.desc}</textarea>
                                </div>
                            </form>
                        `
                    , success: function () {

                    },
                    btn: ['保存', '取消'],
                    btn1: function (index, layero) {
                            var val=$('#updatafrom').serializeArray();
                            layer.confirm('真的修改数据嘛？', function (index) {
                                $.ajax({
                                    type: "post",
                                    url: "updateadminmsg",
                                    data: {
                                        "title": val[0].value,
                                        "type": val[1].value,
                                        "desc": val[2].value,
                                        "id":data.id
                                    },
                                    success:function(datas){
                                        if(datas.code==1){
                                            layer.msg('删除成功!', { icon: 1, time: 1000 });
                                        }else{
                                            layer.msg('程序异常!', { icon: 5, time: 1000 });
                                        }
                                        layer.closeAll();
                                        // layui.table.reload('tabledata');
                                        $(".layui-laypage-btn")[0].click();
                                    },
                                    error:function(err){
                                        layer.msg('程序异常!', { icon: 5, time: 1000 });
                                        layer.closeAll();
                                    }
                                });
                            })
                        }
                    ,
                    btn2: function (index, layero) {
                        layer.close(index);
                    }

                });

            }
        });

        table.on('row(test)', function (obj) {
            if (event.target.name || event.target.parentNode.parentNode.name) {
                return;
            }
            // event.stopPropagation();
            var data = obj.data;
            layer.alert(data.desc,{
                title: '当前行数据：'
            });
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            return false;
        });
    }); 
</script>
<style>
    .layui-table-cell {
        height: auto;
        line-height: 28px;
    }
</style>